package com.authine.cloudpivot.ext.applicationservice.executor;

import com.alibaba.cola.dto.RequestContext;
import com.alibaba.cola.dto.SingleResponse;
import com.alibaba.fastjson.JSONObject;
//import com.authine.cloudpivot.ext.applicationservice.v1.sourcing.HuaBaoService;
import com.authine.cloudpivot.ext.applicationservice.v1.sourcing.PushSXQuotation;
import com.authine.cloudpivot.ext.constants.ModelConst;
import com.authine.cloudpivot.ext.constants.PlatformConst;
import com.authine.cloudpivot.ext.dto.inquiry.InquiryDto;
import com.authine.cloudpivot.ext.enums.inquiry.InquiryStatusEnum;
import com.authine.cloudpivot.ext.enums.system.TriggerMetaStatusEnum;
import com.authine.cloudpivot.ext.utils.BoServiceUtils;
import com.authine.mvp.app.launcher.dto.bo.response.BO;
import com.authine.mvp.app.launcher.dto.bo.response.BOList;
import com.google.common.collect.Maps;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.Lists;

import javax.ws.rs.POST;
import javax.ws.rs.Path;
import java.util.List;
import java.util.Map;

import static com.authine.hermes.app.launcher.service.ApplicationServiceFoundation.CUSTOM_SERVICE;

/**
 * @ClassName InquiryStatusExecutor
 * @Description: 询价单定时任务
 * @Author fj
 * @Date 2021/10/22
 * @Version V1.0
 **/
@Path("InquiryStatusExecutor")
@Slf4j
public class InquiryStatusExecutor {

    /*private final HuaBaoService huaBaoService = new HuaBaoService();*/
    private final PushSXQuotation pushSXQuotation = new PushSXQuotation();

    /**
     * 将过期询价单改为已结束状态
     *
     * @param inquiry 询价单参数
     */
    @Path("updateExpiredInquiry")
    @POST
    @Operation(summary = "将过期询价单改为已结束状态", tags = CUSTOM_SERVICE)
    public void updateExpiredInquiry(@Parameter InquiryDto inquiry) {
        // 设置token认证信息
        RequestContext.get().setCorpLoginId(PlatformConst.corpLoginId);
        RequestContext.get().setLoginId(PlatformConst.loginId);
        RequestContext.get().setLoginName(PlatformConst.loginName);

        log.info("===================定时任务开始，修改询价状态为结束，询价单id：{}===================", inquiry.getInquiryId());
        // 通过询价单获取询价商品
        SingleResponse<BOList> inquiryGoodsBOList = BoServiceUtils.findListBySimple(ModelConst.T_INQUIRY_GOODS, "inquiry_id", inquiry.getInquiryId());
        List<Map<String,Object>> dataList = Lists.newArrayList();

        inquiryGoodsBOList.getData().getData().forEach(inquiryGoods -> {
            Map<String,Object> dataMap = Maps.newHashMap();
            String inquiryGoodsId = String.valueOf(inquiryGoods.get("id"));
            dataMap.put("id", inquiryGoodsId);
            // 修改询价商品状态为已结束
            dataMap.put("inquiry_goods_status", InquiryStatusEnum.INQUIRY_GOODS_4_END.getCode());
            dataList.add(dataMap);
        });

        SingleResponse<BO> updateBO = BoServiceUtils.updateMainBo(ModelConst.T_INQUIRY_GOODS, dataList);
        log.info("===================定时任务结束，修改询价状态为结束，修改结果：{}===================", JSONObject.toJSONString(updateBO));

        // todo 询价截止推送泛微报价信息

        // todo 推送报价给松下
        pushSXQuotation.pushQuotationGoodsToSx(inquiry);
        // todo 推送报价数据给华宝
  /*      huaBaoService.pushQuotationGoodsToHb(inquiry);*/
        // todo 询价截止发送邮件信息给询价人

        // todo 询价截止，询价商品状态为已结束，发送邮件信息给接收人

        // 修改任务触发器状态为已执行
       /* Map<String,Object> triggerMetaMap = Maps.newHashMap();
        triggerMetaMap.put("id", inquiry.getTriggerMetaId());
        triggerMetaMap.put("trigger_status", TriggerMetaStatusEnum.EXECUTED.id);
        BoServiceUtils.updateMainBo(ModelConst.T_TRIGGER_META, triggerMetaMap);*/

        // 清除登录信息
        RequestContext.clear();
    }
}
